//
//Original includes
//
using System;
using System.Collections.Generic;
using System.Text;
//
//Added includes http://www.thescripts.com/forum/thread257358.html
//
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace ResizeThingyConsole
{
    class Program
    {

        static void Main(string[] args)
        {
            foreach (string s in args)
            {

                //string qualityValue = "";
                string basenamePath = "";
                string fullPath = "";
                string newName = "";
                string testValue = "FAIL";
                int smallHeight = 1;
                int smallWidth = 1;

                //float blogWidth = 640; 
                float webWidth = 800;
                //float screenWidth = 1280;

                //float blogFactor; 
                float webFactor;
                //float screenFactor;

                //
                //Extract Filename (basename) and full path
                //
                basenamePath = System.IO.Path.GetFileName(@s);
                fullPath = System.IO.Path.GetDirectoryName(@s);

                //System.Console.WriteLine("Long Name: " + s);
                //System.Console.WriteLine("basename: " + basenamePath);
                //System.Console.WriteLine("full path: " + fullPath);

                //
                //New Code from http://www.thescripts.com/forum/thread257358.html
                //
                Image oldImage = Image.FromFile(@s);

                //
                //Need to create image object based on orientation of photo 800x600 or 600x800
                //
                if (oldImage.Height < oldImage.Width) //Testing orientation of photo
                {
                    //
                    //Landscape image
                    //                   
                    //Defaulting to webSize for now to test
                    //
                    if (oldImage.Width > webWidth) //testing for oldImage being larger than resized target
                    {
                        //
                        //Calculate new shrinkFactor
                        //
                        webFactor = oldImage.Width / webWidth;
                        //
                        //Calculate new height and width for photo
                        //
                        smallHeight = (int)( oldImage.Height / webFactor );
                        smallWidth = (int)( oldImage.Width / webFactor );
                        testValue = "PASS";

                    }
                }
                else
                {
                    //
                    //Portrait or square image
                    //
                    //Defaulting to webSize for now to test
                    //
                    if (oldImage.Height > webWidth) //testing for oldImage being larger than resized target
                    {
                        //
                        //Calculate new shrinkFactor
                        //
                        webFactor = oldImage.Height / webWidth;
                        //
                        //Calculate new height and width for photo
                        //
                        smallHeight = (int)(oldImage.Height / webFactor);
                        smallWidth = (int)(oldImage.Width / webFactor);
                        testValue = "PASS";
                    }

                }

                if (testValue == "PASS")
                {
                    //
                    //Create new (blank) Image object called "thumb" of new smaller size (canvas)
                    //
                    Image thumb = new Bitmap(smallWidth, smallHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    //
                    //Create new Graphics object called "oGraphic" based on object "thumb" with multiple quality attributes
                    //
                    Graphics oGraphic = Graphics.FromImage(thumb);
                    oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                    oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    //
                    //Set attribute of object "oGraphic" to rectangle of new smaller size (photo on canvas)
                    //
                    Rectangle rect = new Rectangle(0, 0, smallWidth, smallHeight);
                    //
                    //Draw the old photo in the new rectangle
                    //
                    oGraphic.DrawImage(oldImage, rect);
                    //
                    //Save as a JPEG
                    //
                    //System.Console.WriteLine(@s);
                    newName = fullPath + "\\" + "small-" + basenamePath;
                    thumb.Save(newName, ImageFormat.Jpeg);

                    //System.Console.WriteLine("Done!");
                }
            }
        }
    }
}
